<?php

/**
 * Description of Attach_model
 *
 * @author febusch
 */
class Attach_model extends CI_Model {
    
    const UPLOAD_PATH = "./uploads/";
    
    /**
     * 
     * @param type Defines the file type family(Default: All)
     * @param type Defines the target folder to upload (Default: tmp)
     * @return boolean/int
     */
    public function upload_file($type, $target_folder, $spawn)
    {
        if(isset($type) AND isset($target_folder))
        {
            
            // Existiert der Ordner? Wenn nicht leg ihn an!
            if(!is_dir(self::UPLOAD_PATH.$target_folder)) {
                mkdir(self::UPLOAD_PATH.$target_folder, 0770);
            }
            
            switch($type)
            {
                case 'text':    $family = 'pdf|doc|docx|txt|rtf|odf';
                                break;
                case 'picture': $family = 'jpg|jpeg|png|gif|tif|tiff|svg';
                                break;
                case 'all':     $family = 'pdf|doc|docx|txt|rtf|odf|jpg|jpeg|png|gif|tif|tiff|svg';
                                break;
                default:        $family = '';
            }
            
            $config['upload_path']      = self::UPLOAD_PATH.$target_folder;
            $config['allowed_types']    = $family;
            $config['max_size']         = 2048; // 2MB
            $config['encrypt_name']     = TRUE;
            
            $this->load->library('upload', $config);
            
            if (!$this->upload->do_upload()) {
                $this->session->set_flashdata('upload_error', $this->upload->display_errors());
                redirect($spawn);
            } else {
                $upload_data = $this->upload->data();
                $filename = $upload_data['file_name'];
                
                // Insert this filename into the database
                //$attachment_id = $this->insert_attachment($target_folder.'/'.$filename);
                $this->session->set_flashdata('attachment', array('filename' => $filename, 'folder' => $target_folder));
                redirect($spawn);
            }
            
        }
        else
            return NULL;
    }
    
    public function insert_attachment($filename)
    {
        $this->db->insert('attachment', array('path' => $filename));
        $query = $this->db->query('SELECT LAST_INSERT_ID() as ID');
        $row = $query->row();
        return $row->ID;
    }
    
    public function move_attachment($filename, $target)
    {
        $tmp_folder = self::UPLOAD_PATH.'tmp/';
        
        if (copy($tmp_folder.$filename,self::UPLOAD_PATH.$target.'/'.$filename)) {
            unlink($tmp_folder.$filename);
          }
    }
    
    public function cleanup()
    {
        $tmp_folder = self::UPLOAD_PATH.'tmp/';
        
        $files = glob($tmp_folder); // get all file names
        foreach($files as $file){ // iterate files
          if(is_file($file) AND $file != 'index.html')
            unlink($file); // delete file
        }
    }
    
}

?>
